import os
from flask import Flask, Blueprint, render_template, request, redirect, session, send_from_directory, current_app
from ..utils import db

hy = Blueprint(name="history", import_name=__name__, url_prefix='/')


@hy.app_template_filter('basename')
def basename(file_path):
    return os.path.basename(file_path)


@hy.route('/uploads/<filename>')
def uploaded_files(filename):
    # 从指定的目录中发送文件
    return send_from_directory(current_app.config['UPLOAD_FOLDER'], filename)


@hy.route("/history", methods=['GET', 'POST'])
def history():
    if request.method == 'GET':
        userinfo_id = session['user_info']['id']
        page = request.args.get("page", "1")
        page = int(page)
        per_page_count = 10
        offset = (page - 1) * per_page_count
        offset = int(offset)
        row_list = db.get_db_user_history_records(userinfo_id, per_page_count, offset)
        print(row_list)
        return render_template("history.html", row_list=row_list)
